package com.sskj.b70.v.activity;

import android.content.Intent;
import android.support.transition.ChangeBounds;
import android.support.transition.Scene;
import android.support.transition.TransitionManager;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.MoneyValueFilter;
import com.sskj.b70.common.widget.ToolBarLayout;
import com.sskj.b70.p.activity.FundInputPresenter;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.base.BaseActivity;

import butterknife.BindView;

/**
 * <pre>
 *     author : lee
 *     desc   : 基金划转
 *     version: 1.0
 * </pre>
 */
public class FundTransferActivity extends BaseActivity<FundInputPresenter> {

    @BindView(R.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.framelayout)
    FrameLayout mFramelayout;
    @BindView(R.id.fund_transfer_count)
    EditText mFundTransferCount;
    @BindView(R.id.fund_transfer_count_all)
    TextView mFundTransferCountAll;
    @BindView(R.id.fund_transfer_count_layout)
    LinearLayout mFundTransferCountLayout;
    @BindView(R.id.fund_balance_value)
    TextView mFundBalanceValue;
    @BindView(R.id.fund_transfer_submit)
    TextView mFundTransferSubmit;
    @BindView(R.id.fund_transfer_switch)
    ImageView mFundTransferSwitch;
    private Scene mSceneb;
    private Scene mScenea;
    private int mStatus;

    /**
     * 1 余额转基金 2 基金赚余额
     */
    private int mType = 1;
    /**
     * 用户钱包金额
     */
    private double mBalance;
    /**
     * 用户基金金额
     */
    private double mFundBalance;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fund_transfer;
    }

    @Override
    public FundInputPresenter getPresenter() {
        return new FundInputPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        mTitle.setText(App.INSTANCE.getString(R.string.strFundTransferActivity0));
        mBalance = getIntent().getDoubleExtra(Constants.INTENT_BALANCE, 0);
        mFundBalance = getIntent().getDoubleExtra(Constants.INTENT_FUND_BALANCE, 0);
        mFundBalanceValue.setText(String.format(App.INSTANCE.getString(R.string.strFundTransferActivity1), String.valueOf(mBalance)));
        mFundTransferCount.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});

        mToolBar.setLeftButtonOnClickLinster(v -> finish());
        mToolBar.setRightButtonIcon(getResources().getDrawable(R.mipmap.fbjl));
        mToolBar.setRightButtonOnClickLinster(v -> {
            startActivity(new Intent(mActivity, FundTransferRecordActivity.class));
        });
        mSceneb = Scene.getSceneForLayout(mFramelayout, R.layout.layout_fund_transfer_b, this);
        mScenea = Scene.getSceneForLayout(mFramelayout, R.layout.layout_fund_transfer_a, this);

        mScenea.setEnterAction(new Runnable() {
            @Override
            public void run() {
                mType = 1;
                mFundBalanceValue.setText(String.format(App.INSTANCE.getString(R.string.strFundTransferActivity1), String.valueOf(mBalance)));
                Log.e(App.INSTANCE.getString(R.string.strFundTransferActivity3), "mScene  a == Enter");
            }
        });
        mSceneb.setEnterAction(new Runnable() {
            @Override
            public void run() {
                mType = 2;
                mFundBalanceValue.setText(String.format(App.INSTANCE.getString(R.string.strFundTransferActivity1), String.valueOf(mFundBalance)));
                Log.e(App.INSTANCE.getString(R.string.strFundTransferActivity3), "mScene  b == Enter");
            }
        });

        ClickUtil.click(mFundTransferSwitch, () -> {
            if (mStatus % 2 == 0){
                TransitionManager.go(mSceneb, new ChangeBounds());
            }else {
                TransitionManager.go(mScenea, new ChangeBounds());
            }
            Log.e(App.INSTANCE.getString(R.string.strFundTransferActivity3), "mStatus ==  " + mStatus);
            mStatus++;
        });
        ClickUtil.click(mFundTransferCountAll, () -> {
            if (mType == 1){
                mFundTransferCount.setText(String.valueOf(mBalance));
            }else {
                mFundTransferCount.setText(String.valueOf(mFundBalance));
            }
        });
        ClickUtil.click(mFundTransferSubmit, () -> {
            String inputValue = mFundTransferCount.getText().toString().trim();

            if (TextUtils.isEmpty(inputValue)){
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFundTransferActivity10));
                return;
            }
            if (Double.valueOf(inputValue) > (mType == 1? mBalance:mFundBalance)){
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFundTransferActivity11));
                return;
            }
            if (Double.valueOf(inputValue) == 0){
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFundTransferActivity12));
                return;
            }
            mPresenter.reuqestSubmitTransfer(inputValue, mType);
        });
    }

    public void onTransferSubmitSuccess(int type, Double aDouble) {
        if (type == 1){//1 余额转基金
            mBalance -=aDouble;
            mFundBalanceValue.setText(String.format(App.INSTANCE.getString(R.string.strFundTransferActivity1), String.valueOf(mBalance)));
        }else {//2 基金转余额
            mFundBalance -=aDouble;
            mFundBalanceValue.setText(String.format(App.INSTANCE.getString(R.string.strFundTransferActivity1), String.valueOf(mFundBalance)));
        }

        mFundTransferCount.setText("");
    }
}
